package base

import (
	"context"
	"github.com/go-redis/redis/v8"
	"log"
)

var redisClient *redis.Client

type RedisService struct {
}

func (this *RedisService) Init() error {
	redisClient = redis.NewClient(&redis.Options{
		Network:            "tcp",
		Addr:               "localhost:6379",
		PoolSize:           20,
		MinIdleConns:       1,
		MaxConnAge:         0,
		PoolTimeout:        1200,
		IdleTimeout:        300,
		IdleCheckFrequency: 5,
	})

	if err := redisClient.Ping(context.Background()).Err(); err != nil {
		return err
	} else {
		log.Println("初始化redis连接成功")
	}

	return nil
}

func (this *RedisService) Close() error {
	if redisClient != nil {
		log.Println("redis连接关闭中...")
		return redisClient.Close()
	}

	return nil
}

// GetRedisClient 获取redisclient
func GetRedisClient() *redis.Client {
	return redisClient
}
